package com.aliyuncs.aui.service.impl;

import com.aliyuncs.aui.common.utils.MySQLUtil;
import com.aliyuncs.aui.dto.res.RestaurantInfo;
import com.aliyuncs.aui.dto.res.MenuDTO;
import com.aliyuncs.aui.service.RestoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class RestoServiceImpl implements RestoService {

    private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");

    private Date parseTimeString(String timeStr) {
        try {
            if (timeStr == null) {
                return null;
            }
            if ("24:00:00".equals(timeStr)) {
                timeStr = "23:59:59";
            }
            return TIME_FORMAT.parse(timeStr);
        } catch (ParseException e) {
            log.warn("时间格式解析失败: {}", timeStr, e);
            return null;
        }
    }

    private final MySQLUtil mySQLUtil;

    public RestoServiceImpl(MySQLUtil mySQLUtil) {
        this.mySQLUtil = mySQLUtil;
    }

    @Override
    public List<MenuDTO> getMenusByTenantId(String tenantId) {
        String sql = "SELECT * FROM menu WHERE tenant_id = ?";
        List<MenuDTO> menus = new ArrayList<>();
        try (ResultSet rs = mySQLUtil.executeQuery(sql, tenantId)) {
            while (rs.next()) {
                menus.add(mapResultSetToMenuDTO(rs));
            }
        } catch (SQLException e) {
            log.error("查询菜单失败", e);
        }
        return menus;
    }

    private MenuDTO mapResultSetToMenuDTO(ResultSet rs) throws SQLException {
        MenuDTO menu = new MenuDTO();
        menu.setId(rs.getInt("id"));
        menu.setName(rs.getString("name"));
        menu.setCategory(rs.getString("category"));
        menu.setPrice(rs.getBigDecimal("price"));
        menu.setImage(rs.getString("image"));
        menu.setDescription(rs.getString("description"));
        menu.setIconUrl(rs.getString("icon_url"));
        menu.setVideoUrl(rs.getString("video_url"));
        menu.setStatus(rs.getString("status"));
        menu.setTenantId(rs.getString("tenant_id"));
        menu.setIfd(rs.getString("ifd"));
        menu.setCreatedAt(rs.getTimestamp("created_at"));
        menu.setUpdatedAt(rs.getTimestamp("updated_at"));
        menu.setMenuId(rs.getString("menu_id"));
        return menu;
    }

    @Override
    public RestaurantInfo getRestoInfo(String tenantId) {
        String sql = "SELECT * FROM resto WHERE tenant_id = ?";
        try (ResultSet rs = mySQLUtil.executeQuery(sql, tenantId)) {
            if (rs.next()) {
                RestaurantInfo info = new RestaurantInfo();
                info.setId(rs.getInt("id"));
                info.setName(rs.getString("name"));
                info.setDept(rs.getString("dept"));
                info.setAddr(rs.getString("addr"));
                info.setTel(rs.getString("tel"));
                info.setOpHrs(parseTimeString(rs.getString("op_hrs")));
                info.setClHrs(parseTimeString(rs.getString("cl_hrs")));
                info.setPicUrl(rs.getString("pic_url"));
                info.setCreatedAt(rs.getTimestamp("created_at"));
                info.setUpdatedAt(rs.getTimestamp("updated_at"));
                info.setTenantId(rs.getString("tenant_id"));
                return info;
            }
        } catch (SQLException e) {
            log.error("获取餐厅信息失败：", e);
            throw new RuntimeException("获取餐厅信息失败", e);
        }
        return null;
    }
}
